
#ifndef _MEMBER_BICON_H_
#define _MEMBER_BICON_H_

#include <BMap>
#include <BTable>
#include <BImage>

#define icon_member(icon)		((member_BIcon*)((BIcon*)icon)->_ptr)

namespace BWE
{
	BImage* ConvertRGBA(BImage* image, int state);
	BImage* ConvertRGB(BImage* image, int state);
	BImage* ConvertGray(BImage* image, int state);
	BImage* ConvertAlpha(BImage* image, int state);
	BImage* ConvertGrayAlpha(BImage* image, int state);
	inline BImage* ConvertImage(BImage* image, int state)
	{
		if (!image)
			return 0;
		switch (image->format())
		{
		case Format_RGBA:
			return ConvertRGBA(image, state);
			break;
		case Format_RGB:
			return ConvertRGB(image, state);
			break;
		case Format_Gray:
			return ConvertGray(image, state);
			break;
		case Format_Alpha:
			return ConvertAlpha(image, state);
			break;
		case Format_Gray_Alpha:
			return ConvertGrayAlpha(image, state);
			break;
		}
		return 0;
	}

	inline State StringToState(const BString& text)
	{
		State state = State_None;
		if (text.contain("-active") || text.contain("-actived") || text.contain("_active") || text.contain("_actived"))
			state = State_Actived;
		else if (text.contain("-check") || text.contain("-checked") || text.contain("_check") || text.contain("_checked"))
			state = State_Checked;
		else if (text.contain("-disable") || text.contain("-disabled") || text.contain("_disable") || text.contain("_disabled"))
			state = State_Disable;
		else if (text.contain("-normal") || text.contain("_normal"))
			state = State_Normal;

		if (text.contain("-hover") || text.contain("-hovered") || text.contain("_hover") || text.contain("_hovered"))
			state |= State_Hovered;
		if (text.contain("-press") || text.contain("-pressed") || text.contain("_press") || text.contain("_pressed"))
			state |= State_Pressed;
		if (text.contain("-select") || text.contain("-selected") || text.contain("_select") || text.contain("_selected"))
			state |= State_Selected;
		return state;
	}
	inline bool SplitImage(const BImage* image, BTable<BImageHolder>& imageTable)
	{
		int row = imageTable.row();
		int column = imageTable.column();
		if (row < 1 || column < 1)
			return false;
		int width = image->width() / column;
		int height = image->height() / row;
		for (int r = 0; r < row; r++)
		{
			int y = r * height;
			for (int c = 0; c < column; c++)
			{
				int x = c * width;
				BImage* subimg = new BImage(width, height, image->format());
				image->clone(x, y, width, height, subimg);
				imageTable[r][c] = subimg;
			}
		}
		return true;
	}
	inline void InitImageMap(BMap<State, BImageHolder>& imageMap)
	{
		if (imageMap[State_None] == 0)
			imageMap[State_None] = imageMap[State_Normal];

		if (BImage* image = imageMap[State_Hovered])
		{
			if (imageMap(State_Normal_Hovered) == 0)
				imageMap[State_Normal_Hovered] = image;
			if (imageMap(State_Actived_Hovered) == 0)
				imageMap[State_Actived_Hovered] = image;
			if (imageMap(State_Checked_Hovered) == 0)
				imageMap[State_Checked_Hovered] = ConvertImage(image, State_Checked);
			if (imageMap(State_Disable_Hovered) == 0)
				imageMap[State_Disable_Hovered] = ConvertImage(image, State_Disable);
		}

		if (BImage* image = imageMap[State_Pressed])
		{
			if (imageMap(State_Normal_Pressed) == 0)
				imageMap[State_Normal_Pressed] = image;
			if (imageMap(State_Actived_Pressed) == 0)
				imageMap[State_Actived_Pressed] = image;
			if (imageMap(State_Checked_Pressed) == 0)
				imageMap[State_Checked_Pressed] = ConvertImage(image, State_Checked);
			if (imageMap(State_Disable_Pressed) == 0)
				imageMap[State_Disable_Pressed] = ConvertImage(image, State_Disable);
		}

		if (BImage* image = imageMap[State_Selected])
		{
			if (imageMap(State_Normal_Selected) == 0)
				imageMap[State_Normal_Selected] = image;
			if (imageMap(State_Actived_Selected) == 0)
				imageMap[State_Actived_Selected] = image;
			if (imageMap(State_Checked_Selected) == 0)
				imageMap[State_Checked_Selected] = ConvertImage(image, State_Checked);
			if (imageMap(State_Disable_Selected) == 0)
				imageMap[State_Disable_Selected] = ConvertImage(image, State_Disable);
		}

		if (BImage* image = imageMap[State_None])
		{
			if (imageMap(State_Hovered) == 0)
				imageMap[State_Hovered] = ConvertImage(image, State_Hovered);
			if (imageMap(State_Pressed) == 0)
				imageMap[State_Pressed] = ConvertImage(image, State_Pressed);
			if (imageMap(State_Selected) == 0)
				imageMap[State_Selected] = ConvertImage(image, State_Selected);

			if (imageMap(State_Normal) == 0)
				imageMap[State_Normal] = image;
			if (imageMap(State_Actived) == 0)
				imageMap[State_Actived] = image;
			if (imageMap(State_Checked) == 0)
				imageMap[State_Checked] = ConvertImage(image, State_Checked);
			if (imageMap(State_Disable) == 0)
				imageMap[State_Disable] = ConvertImage(image, State_Disable);
		}

		if (BImage* image = imageMap[State_Normal])
		{
			if (imageMap(State_Normal_Hovered) == 0)
				imageMap[State_Normal_Hovered] = ConvertImage(image, State_Hovered);
			if (imageMap(State_Normal_Pressed) == 0)
				imageMap[State_Normal_Pressed] = ConvertImage(image, State_Pressed);
			if (imageMap(State_Normal_Selected) == 0)
				imageMap[State_Normal_Selected] = ConvertImage(image, State_Selected);
		}

		if (BImage* image = imageMap[State_Actived])
		{
			if (imageMap(State_Actived_Hovered) == 0)
				imageMap[State_Actived_Hovered] = ConvertImage(image, State_Hovered);
			if (imageMap(State_Actived_Pressed) == 0)
				imageMap[State_Actived_Pressed] = ConvertImage(image, State_Pressed);
			if (imageMap(State_Actived_Selected) == 0)
				imageMap[State_Actived_Selected] = ConvertImage(image, State_Selected);
		}

		if (BImage* image = imageMap[State_Checked])
		{
			if (imageMap(State_Checked_Hovered) == 0)
				imageMap[State_Checked_Hovered] = ConvertImage(image, State_Hovered);
			if (imageMap(State_Checked_Pressed) == 0)
				imageMap[State_Checked_Pressed] = ConvertImage(image, State_Pressed);
			if (imageMap(State_Checked_Selected) == 0)
				imageMap[State_Checked_Selected] = ConvertImage(image, State_Selected);
		}

		if (BImage* image = imageMap[State_Disable])
		{
			if (imageMap(State_Disable_Hovered) == 0)
				imageMap[State_Disable_Hovered] = ConvertImage(image, State_Hovered);
			if (imageMap(State_Disable_Pressed) == 0)
				imageMap[State_Disable_Pressed] = ConvertImage(image, State_Pressed);
			if (imageMap(State_Disable_Selected) == 0)
				imageMap[State_Disable_Selected] = ConvertImage(image, State_Selected);
		}

	}

	class member_BIcon
	{
	public:
		member_BIcon(BIcon* icon);
		~member_BIcon();

		void makeImage(State state);

		BIcon*			boss;
		BString			fileName;
		BImageHolder	images[State_Max];
	};
}

#endif
